module Admin::BaseHelper
  
  def main_nav
    menu_nav = [link_to("#{t(:a_global_settings)}", "/admin/admin", :class => nav1_class ),
                link_to("#{t(:a_application_mement)}", "/admin/app", :class => app_nav_class),
                link_to("#{t(:a_m_member_oprate)}", "/admin/member", :class => member_nav_class),
                link_to("#{t(:a_m_order_mengament)}", "/admin/order", :class => order_nav_class), 
                link_to("#{t(:a_m_goods_management)}", "/admin/goods", :class => goods_nav_class),
                link_to("#{t(:a_m_ad_management)}", "/admin/ad", :class => ad_nav_class),
                link_to("#{t(:a_m_article_management)}", "/admin/article", :class => article_nav_class),
                link_to("#{t(:a_m_datastats)}", "/admin/statistic", :class => statistic_nav_class),
                link_to("#{t(:a_m_promotion)}", "/admin/promotion", :class => promotion_nav_class),
    ]
    content_tag "ul" do
      str = ""
      menu_nav.each do |item|
        str << (content_tag "li" do
          item
        end
        )
      end
      str
    end
  end
  
  CONTROLLERS = ['admin', 'member', 'setting', 'area', 'order', 
                 'article', 'promotion', 'goods', 'ad', 'statistic',
                 'app']
  
  CONTROLLERS.each do |controller|
    define_method("#{controller}_nav_class") do |*args|
      case params[:controller]
        when "admin/#{controller}"
          'active'
        else
          nil
      end
    end
  end
  
  def nav1_class
    admin_nav_class || setting_nav_class || area_nav_class
  end
  
  def sub_navi
    controller = params[:controller].to_s.split("/")[1] rescue ""
    case controller
      when /^(admin|setting|area)$/
        admin_sub_navi
      when /^(member)$/
        member_sub_navi
      when /^(order)$/
        order_sub_navi
      when /^(ad)$/
        ad_sub_navi
      when /^(app)$/
        app_sub_navi
    end
  end
  
  def admin_sub_navi
    content_tag 'ul', :class => 'submenu' do
      str = ''
      str << (content_tag 'li', :class => "#{'active' if params[:controller] == 'admin/admin' && params[:action] == 'index'}" do
        link_to("#{t(:a_manager_index)}", '/admin/admin')
      end
      )
      str << (content_tag 'li', :class => "#{'active' if params[:controller] == 'admin/setting' && params[:action] == 'index'}" do
        link_to("#{t(:a_set_site)}", '/admin/setting')
      end
      )
      str << (content_tag 'li', :class => "#{'active' if params[:controller] == 'admin/setting' && params[:action] == 'index_image'}" do
        link_to("#{t(:a_index_images)}", '/admin/setting/index_image')
      end
      )
      str << (content_tag 'li', :class => "#{'active' if params[:controller] == 'admin/area' && params[:action] == 'index'}" do
        link_to("#{t(:a_sys_area)}", '/admin/area')
      end
      )
    end
  end
  
  def member_sub_navi
    content_tag "ul", :class => "submenu" do
      str = ""
      str << (content_tag 'li', :class => "#{'active' if params[:controller] == 'admin/member' && params[:action] == 'index'}" do
        link_to("#{t(:a_memeber_list)}", "/admin/member")
      end
      )
      str << (content_tag 'li', :class => "#{'active' if params[:controller] == 'admin/member' && (%w{member_rank add_member_rank edit_member_rank}.include?(params[:action]))}" do
        link_to("#{t(:a_user_rank)}", "/admin/member/member_rank")
      end
      )
      str << (content_tag 'li', :class => "#{'active' if params[:controller] == 'admin/member' && params[:action] == 'csv_export'}" do
        link_to("#{t(:a_memeber_data_export)}", "/admin/member/csv_export")
      end
      )
      str << (content_tag 'li', :class => "#{'active' if params[:controller] == 'admin/member' && params[:action] == 'csv_import'}" do
        link_to("#{t(:a_memeber_data_import)}", "/admin/member/csv_import")
      end
      )
      str << (content_tag 'li', :class => "#{'active' if params[:controller] == 'admin/member' && (%w{admin_list add_admin create_admin edit_admin update_admin}.include?(params[:action]))}" do
        link_to("#{t(:a_admin_list)}", "/admin/member/admin_list")
      end
      )
    end
  end
  
  def order_sub_navi
    content_tag "ul", :class => "submenu" do
      str = ""
      str << (content_tag 'li', :class => "#{'active' if params[:controller] == 'admin/order' && params[:action] == 'index'}" do
        link_to("#{t(:a_order_list)}", "/admin/order")
      end
      )
    end
  end
  
  def ad_sub_navi
    content_tag "ul", :class => "submenu" do
      str = ""
      str << (content_tag 'li', :class => "#{'active' if params[:controller] == 'admin/ad' && params[:action] == 'index'}" do
        link_to("#{t(:a_adposition_list)}", "/admin/ad")
      end
      )
    end
  end
  
  def app_sub_navi
    content_tag "ul", :class => "submenu" do
      str = ""
      str << (content_tag 'li', :class => "#{'active' if params[:controller] == 'admin/app' && params[:action] == 'index'}" do
        link_to("#{t(:a_adposition_list)}", "/admin/app")
      end
      )
      str << (content_tag 'li', :class => "#{'active' if params[:controller] == 'admin/app' && params[:action] == 'navigation'}" do
        link_to("#{t(:a_m_navigation)}", "/admin/app/navigation")
      end
      )
    end
  end
  
end
